package com.caseProcess.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caseProcess.entity.*;
import com.caseProcess.service.PageService;
import com.caseProcess.service.VCaseOperNoteService;
import com.caseProcess.service.VCaseProcInfoByPoliceService;
import com.caseProcess.utils.IdWorker;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/VCaseProcInfoByPolice")
public class VCaseProcInfoByPoliceController {

    @Resource
    private VCaseProcInfoByPoliceService vCaseProcInfoByPoliceService;
    @Resource
    private PageService pageService;


    /**
     * 获取当前部门负责的民警信息
     *
     * @param procTatus
     * @param processId
     * @param request
     * @return
     */
    @RequestMapping("/getCaseProcInfoByDepartment")
    public Result getCaseProcInfoByDepartment(int procTatus,String processId,HttpServletRequest request) {
        Result result=new Result();
        List<VCaseProcInfoByPolice> list=new ArrayList<VCaseProcInfoByPolice>();
        VCaseProcInfoByPolice vCaseProcInfoByPolice=new VCaseProcInfoByPolice();
        vCaseProcInfoByPolice.setParentId("0");//为0的是派出所指派给民警的那一条数据
        Object object=request.getSession().getAttribute("police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        VUserInfo vUserInfo= (VUserInfo) object;
        vCaseProcInfoByPolice.setProcDepartmentId(vUserInfo.getDepartmentId());
        if(procTatus==99) {//当传入的是99则表示查询所有指挥中心和领导审核中的警情
            vCaseProcInfoByPolice.setProcTatus(3);
        }else{
            vCaseProcInfoByPolice.setProcTatus(procTatus);
        }
        vCaseProcInfoByPolice.setProcessId(processId);
        list=vCaseProcInfoByPoliceService.selectVCaseProcInfoByPolices(vCaseProcInfoByPolice);
        if(procTatus==99) {//当传入的是99则表示查询所有指挥中心和领导审核中的警情
            vCaseProcInfoByPolice.setProcTatus(4);
            List<VCaseProcInfoByPolice> listBy4=vCaseProcInfoByPoliceService.selectVCaseProcInfoByPolices(vCaseProcInfoByPolice);
            for(int i=0;i<listBy4.size();i++){
                list.add(listBy4.get(i));//合并list
            }
        }
        result.setData(list);
        result.setCount(list.size());
        return result;
    }

    /**
     * 获取警员不同状态的分派信息
     *
     * @param policeId  民警id
     * @param procTatus ==0 查询全部不为0(为指派)且不为5(结案)
     * @param processId 当有processId的时候 用于查询当前这个分派的警情所处理的警员
     * @return
     */
    @RequestMapping("/getCaseProcInfoByPolice")
    public Result getCaseProcInfoByPolice(String policeId,int procTatus,String processId) {
        Result result=new Result();
        List<VCaseProcInfoByPolice> list=new ArrayList<VCaseProcInfoByPolice>();
        VCaseProcInfoByPolice vCaseProcInfoByPolice=new VCaseProcInfoByPolice();
        vCaseProcInfoByPolice.setParentId("0");//为0的是派出所指派给民警的那一条数据
        vCaseProcInfoByPolice.setNextPoliceId(policeId);
        if(procTatus==99) {//当传入的是99则表示查询所有指挥中心和领导审核中的警情
            vCaseProcInfoByPolice.setProcTatus(3);
        }else{
            vCaseProcInfoByPolice.setProcTatus(procTatus);
        }
        vCaseProcInfoByPolice.setProcessId(processId);
        list=vCaseProcInfoByPoliceService.selectVCaseProcInfoByPolices(vCaseProcInfoByPolice);
        if(procTatus==99) {//当传入的是99则表示查询所有指挥中心和领导审核中的警情
            vCaseProcInfoByPolice.setProcTatus(4);
            List<VCaseProcInfoByPolice> listBy4=vCaseProcInfoByPoliceService.selectVCaseProcInfoByPolices(vCaseProcInfoByPolice);
            for(int i=0;i<listBy4.size();i++){
                list.add(listBy4.get(i));//合并list
            }
        }
        result.setData(list);
        result.setCount(list.size());
        return result;
    }


    /**
     * 获取对应分派案情的所有回退信息
     *
     * @param processId
     * @return
     */
    @RequestMapping("/getOperNoteByBack")
    public Result getOperNoteByBack(String processId) {
        Result result=new Result();
        List<VCaseProcInfoByPolice> list=new ArrayList<VCaseProcInfoByPolice>();
        VCaseProcInfoByPolice vCaseProcInfoByPolice=new VCaseProcInfoByPolice();
        vCaseProcInfoByPolice.setProcessId(processId);
        vCaseProcInfoByPolice.setResultStatusId(1);
        vCaseProcInfoByPolice.setBack(true);
        list=vCaseProcInfoByPoliceService.selectVCaseProcInfoByPolices(vCaseProcInfoByPolice);
        list.remove(0);//第一条为指派信息不是回退信息
        result.setData(list);
        result.setCount(list.size());
        return result;
    }


    /**
     * 获取分派的所有处理过程
     * @param processId
     * @return
     */
    @RequestMapping("/getCaseOperNote")
    public Result getCaseOperNote(String processId) {
        Result result=new Result();
        List<VCaseProcInfoByPolice> list=new ArrayList<VCaseProcInfoByPolice>();
        VCaseProcInfoByPolice vCaseProcInfoByPolice=new VCaseProcInfoByPolice();
        vCaseProcInfoByPolice.setProcessId(processId);
        list=vCaseProcInfoByPoliceService.selectVCaseOperNotes(vCaseProcInfoByPolice);
        result.setData(list);
        result.setCount(list.size());
        return result;
    }



    /**
     *
     * 待审批列表
     * @param nextPoliceId
     * @return
     */
    @RequestMapping("/selectVCaseProcInfoByPolicesStatus")
    public Result selectVCaseProcInfoByPolicesStatus(String nextPoliceId){
        Result result=new Result();
        List<VCaseProcInfoByPolice> list= new ArrayList<VCaseProcInfoByPolice>();
        list=vCaseProcInfoByPoliceService.selectVCaseProcInfoByPolicesStatus(nextPoliceId);
        Map<String,VCaseProcInfoByPolice> map=new HashMap<String, VCaseProcInfoByPolice>();
        for(int i=0;i<list.size();i++){
            map.put(list.get(i).getProcessId(),list.get(i));
        }
        list.clear();
        for(String key:map.keySet()){
            list.add(map.get(key));
        }
        if (list.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(list);
            result.setCount(list.size());
        } else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }
    /**
     * 指挥中心办结页面使用到
     * 警情办理跟踪
     * @param selectConditionJson 查询条件数据
     * @param page 页码
     * @param limit 数据条数
     * @return 对应数据
     */
    @RequestMapping("/getPoliceTracking")
    public Result getPoliceTracking(String selectConditionJson, int page, int limit) {

        SelectCaseStausCondition selectCaseStausCondition = JSONObject.parseObject(selectConditionJson, SelectCaseStausCondition.class);

        String strWhere = "1=1";
        if (!selectCaseStausCondition.getCaseId().equals("") && !selectCaseStausCondition.getCaseId().equals(null)) {
            strWhere += " and caseid like concat('%','" + selectCaseStausCondition.getCaseId() + "','%')";
        }
        if (!selectCaseStausCondition.getProcLevel().equals("") && !selectCaseStausCondition.getProcLevel().equals(null)) {
            strWhere += " and proclevel = '" + selectCaseStausCondition.getProcLevel() + "'";
        }
        if (!selectCaseStausCondition.getCaseDescription().equals("") && !selectCaseStausCondition.getCaseDescription().equals(null)) {
            strWhere += " and casedescription like concat('%','" + selectCaseStausCondition.getCaseDescription()+ "','%')";
        }
        if (selectCaseStausCondition.getResultStatusId()==999) {
            strWhere += " and resultstatusid != 5 ";
        }else if (selectCaseStausCondition.getResultStatusId()==1) {
            strWhere += " and resultstatusid = 0 or resultstatusid= 1";
        }else {
            strWhere += " and resultstatusid = '" + selectCaseStausCondition.getResultStatusId() + "'";
        }
        if (!selectCaseStausCondition.getStartTime().equals("") && !selectCaseStausCondition.getStartTime().equals(null)) {
            strWhere += " and askForTime >='" + selectCaseStausCondition.getStartTime() + "'";
        }
        if (!selectCaseStausCondition.getEndTime().equals("") && !selectCaseStausCondition.getEndTime().equals(null)) {
            strWhere += " and askForTime <='" + selectCaseStausCondition.getEndTime() + "'";
        }

        Page pg = new Page("V_CaseProcInfoNew", "caseid", 1, true, "*", limit, page, strWhere);

        List<Map<String, Object>> maps = pageService.selectByPage(pg);
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(maps);
        List<VCaseProcInfoByPolice> list = jsonArray.toJavaList(VCaseProcInfoByPolice.class);

        Result result = new Result();
        if (list.size() > 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(list);
            result.setCount(Long.parseLong(maps.get(0).get("PageCount") + ""));
            return result;
        } else if (list.size() == 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("无数据");
        } else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }
    /**
     * 催办分页用到
     * @param selectConditionJson 查询条件数据
     * @param page 页码
     * @param limit 数据条数
     * @return 对应数据
     */
    @RequestMapping("/getUrging")
    public Result getUrging(String selectConditionJson, int page, int limit) {

        SelectCaseUrgingCondition selectCaseUrgingCondition = JSONObject.parseObject(selectConditionJson, SelectCaseUrgingCondition.class);

        String strWhere = "resultstatusid =2";
        if (!selectCaseUrgingCondition.getCaseId().equals("") && !selectCaseUrgingCondition.getCaseId().equals(null)) {
            strWhere += " and caseid like concat('%','" + selectCaseUrgingCondition.getCaseId() + "','%')";
        }
        if (!selectCaseUrgingCondition.getProcLevel().equals("") && !selectCaseUrgingCondition.getProcLevel().equals(null)) {
            strWhere += " and proclevel = '" + selectCaseUrgingCondition.getProcLevel() + "'";
        }
        if (!selectCaseUrgingCondition.getCaseDescription().equals("") && !selectCaseUrgingCondition.getCaseDescription().equals(null)) {
            strWhere += " and casedescription like concat('%','" + selectCaseUrgingCondition.getCaseDescription() + "','%')";
        }
        if (!selectCaseUrgingCondition.getStartTime().equals("") && !selectCaseUrgingCondition.getStartTime().equals(null)) {

            strWhere += " and askForTime >='" + selectCaseUrgingCondition.getStartTime() + "'";
        }
        if (!selectCaseUrgingCondition.getEndTime().equals("") && !selectCaseUrgingCondition.getEndTime().equals(null)) {
            strWhere += " and askForTime <='" + selectCaseUrgingCondition.getEndTime() + "'";
        }

        Page pg = new Page("V_CaseProcInfoNew", "caseid", 1, true, "*", limit, page, strWhere);

        List<Map<String, Object>> maps = pageService.selectByPage(pg);
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(maps);
        List<VCaseProcInfoByPolice> list = jsonArray.toJavaList(VCaseProcInfoByPolice.class);

        Result result = new Result();
        if (list.size() > 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(list);
            result.setCount(Long.parseLong(maps.get(0).get("PageCount") + ""));
            return result;
        } else if (list.size() == 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("无数据");
        } else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     *
     * 待审批详情页
     * @param processId
     * @return
     */
    @RequestMapping("/selectVCaseProcInfoByStatusPro")
    public Result selectVCaseProcInfoByStatusPro(String processId){
        Result result=new Result();
        List<VCaseProcInfoByPolice> list= new ArrayList<VCaseProcInfoByPolice>();
        list=vCaseProcInfoByPoliceService.selectVCaseProcInfoByStatusPro(processId);
        Map<String,VCaseProcInfoByPolice> map=new HashMap<String, VCaseProcInfoByPolice>();
        for(int i=0;i<list.size();i++){
            map.put(list.get(i).getProcessId(),list.get(i));
        }
        list.clear();
        for(String key:map.keySet()){
            list.add(map.get(key));
        }
        if (list.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(list);
            result.setCount(list.size());
        } else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     * 按条件查询审批警情
     * @param nextpoliceId 审批人
     * @param caseId 案件编号
     * @param procLevel 督办等级
     * @param caseDescription 警情描述
     * @param startTime 选择的开始时间
     * @param endTime 选择的结束时间
     * 查询的是在以上两个时间范围内的相关分派时间的案件
     */
    @CrossOrigin
    @RequestMapping("/selectCheckByStament")
    public Result selectCheckByStament(String nextpoliceId,String caseId,String procLevel,String caseDescription,String startTime,String endTime){
        Result result=new Result();
        List<VCaseProcInfoByPolice> list= new ArrayList<VCaseProcInfoByPolice>();
        VCaseProcInfoByPolice vCaseProcInfoByPolice=new VCaseProcInfoByPolice();
        vCaseProcInfoByPolice.setNextPoliceId(nextpoliceId);
        vCaseProcInfoByPolice.setCaseId(caseId);
        vCaseProcInfoByPolice.setProcLevel(procLevel);
        vCaseProcInfoByPolice.setCaseDescription(caseDescription);
        list=vCaseProcInfoByPoliceService.selectCheckByStament(vCaseProcInfoByPolice,startTime,endTime);
        if (list.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(list);
            result.setCount(list.size());
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }
}
